package com.lovo.javafxComponent;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Accordion;
import javafx.scene.control.ListView;
import javafx.scene.control.TitledPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

/**
 * 手风琴组件
 * @author lenovo
 *
 */
public abstract class LovoAccordion extends Accordion{
	/**当前显示列表*/
	private ListView currentListView;
	
	/**
	 * 构造方法
	 * @param infoArray 信息字符串，每个元素格式为“资料管理:员工管理,考勤管理”
	 * @param width 组件宽度
	 * @param height 组件高度
	 */
	public LovoAccordion(String[] infoArray,int width,int height) {
		TitledPane[] titledPaneArray = new TitledPane[infoArray.length];
		for(int i=0;i<infoArray.length;i++) {
			String[] array = infoArray[i].split(":");
			titledPaneArray[i] = new TitledPane(array[0],this.getListView(array[1]));
			titledPaneArray[i].setFont(new Font(20));
			titledPaneArray[i].setTextFill(new Color(0,0,1,1));
		}
		
		this.getPanes().addAll(titledPaneArray);
        this.setExpandedPane(titledPaneArray[0]);
        currentListView = (ListView)titledPaneArray[0].getContent();
        
		this.setMaxHeight(height);
        this.setPrefWidth(width);
        
        
        this.expandedPaneProperty().addListener(
                ( ov, old_val, new_val) -> {
                	if(new_val !=null) {
                	currentListView = (ListView)new_val.getContent();
                	
                	}
                	if(old_val != null) {
                		ListView oldList = (ListView)old_val.getContent();
                    	oldList.getSelectionModel().clearSelection();
                	}
            });
	}
	
	private ListView getListView(String itemInfo) {
		ObservableList list = 
		        FXCollections.observableArrayList();
		String[] infoArray = itemInfo.split(",");
		for(String info : infoArray) {
			list.add(info);
		}
		
		ListView listView = new ListView(list);
		listView.setStyle("-fx-font-size:20px");
		listView.setOnMouseClicked(new EventHandler<Event>() {

			@Override
			public void handle(Event event) {
				Object selectObj = listView.getSelectionModel().getSelectedItem();
				if(selectObj != null) {
					listSelectEvent(selectObj.toString());
				}
			}
		});
		return listView;
	}
	
	public abstract void listSelectEvent(String itemName);
}
